package com.gray.wow.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gray.wow.biz.PhotoBiz;
import com.gray.wow.common.Page;
import com.gray.wow.dao.impl.PhotoDaoImpl;
import com.gray.wow.domain.PageModel;
import com.gray.wow.domain.Photo;

@Service("photoBiz")
@Transactional
public class PhotoBizImpl implements PhotoBiz {
	@Resource
	private PhotoDaoImpl photoDao;

	@Override
	public PageModel getPhoto(Integer pageSize, Integer page) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = 0;
		if (pageSize != null) {
			page = page == null ? 1 : page;
			count = photoDao.findCountByCondition(map);
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
		}
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		List<Photo> list = photoDao.findListByCondition(map);
		PageModel pm = PageModel.getPageModel(pageSize, page, count, list);
		return pm;
	}

	@Override
	public boolean savePhoto(Photo photo) {
		if (photo != null) {
			if (photo.getId() != null && !photo.getId().equals("")) {
				if(photoDao.update(photo)==1)return true;
			} else if (!photo.getNeturl().equals("")
					|| !photo.getLocalurl().equals("")) {
				if(photoDao.add(photo)==1)return true;
			}
		}
return false;
	}

	@Override
	public boolean delPhoto(int... ids) {
		if (ids != null && ids.length>0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ids", ids);
			if(photoDao.delete(map)==ids.length)return true;
		}
return false;
	}

	@Override
	public Photo getPhotoById(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<Photo> list = photoDao.findListByCondition(map);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Photo> getIndexAdvertise() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageSize", Page.PAGESIZE_INDEX_ADVERTISE);
		map.put("startId", 0);
		List<Photo> list = photoDao.findListByCondition(map);
		return list;
	}

}
